package com.maya.screens;


import com.maya.R;
import com.maya.bluetooth.Bluetooth;
import com.maya.screens.ProtocolTypesDialogRadio.AlertPositiveListener;
import com.maya.utilities.Utilities;

import android.app.Activity;
import android.app.FragmentManager;
import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager.NameNotFoundException;
import android.content.res.Resources;
import android.graphics.Color;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

public class MainScreen extends Activity implements OnClickListener, AlertPositiveListener  
{	
	private TextView tvHello;
	private Button buttonNewGameSinglePlayer;
	private Button buttonNewGameTwoPlayer;
	private Button buttonOptions;
	private Button buttonHowToPlay;
	private Button buttonBestScores;
	private Button buttonAbout;
	private Button buttonExit;
	private TextView tvVersion;
	
	private int twoPlayersProtocolTypePosition;
	
	Utilities util = new Utilities();
	PreferencesScreen pref = new PreferencesScreen();
	
	private MediaPlayer sound;
	private boolean soundIsOn = true;
	
    @Override
    public void onCreate(Bundle savedInstanceState) 
    {	
    	pref.setLanguageFromPreferences(this);
    	
    	if(pref.getSoundPreference(getApplicationContext())!=0)
			soundIsOn = true;
		else
			soundIsOn = false;
    	
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
         
        tvHello = (TextView) findViewById(R.id.textViewHello);
        tvHello.setTextColor(Color.WHITE);
        
        buttonNewGameSinglePlayer = (Button) findViewById(R.id.buttonNewGameSinglePlayer);
        buttonNewGameSinglePlayer.setOnClickListener(this);
        
        buttonNewGameTwoPlayer = (Button) findViewById(R.id.buttonNewGameTwoPlayer);
        buttonNewGameTwoPlayer.setOnClickListener(this);
        
        buttonOptions = (Button) findViewById(R.id.buttonOptions);
        buttonOptions.setOnClickListener(this);
        
        buttonHowToPlay = (Button) findViewById(R.id.buttonHowToPlay);
        buttonHowToPlay.setOnClickListener(this);
        
        buttonBestScores = (Button) findViewById(R.id.buttonBestScores);
        buttonBestScores.setOnClickListener(this);
        
        buttonAbout = (Button) findViewById(R.id.buttonAbout);
        buttonAbout.setOnClickListener(this);
        
        buttonExit = (Button) findViewById(R.id.buttonExit);
        buttonExit.setOnClickListener(this);
        
        tvVersion = (TextView) findViewById(R.id.textViewVersion);
        tvVersion.setTextColor(Color.argb(255,0,255,128));
        
        String versionName = "";
        PackageInfo packageInfo;
        Resources res = this.getResources();
        String s = res.getString(R.string.version);
        try {
            packageInfo = getPackageManager().getPackageInfo(getPackageName(), 0);
            versionName = s + ": " + packageInfo.versionName;
        } catch (NameNotFoundException e) {
            e.printStackTrace();
        }
       
        tvVersion.setText(versionName);
        
    }

	public void onClick(View v) 
	{		
		if (v == buttonNewGameSinglePlayer) 
		{
			startActivity(new Intent(MainScreen.this, GameScreenSinglePlayer.class));		
		}
		else if (v == buttonNewGameTwoPlayer) 
		{			
			FragmentManager manager = getFragmentManager();
			
            ProtocolTypesDialogRadio alert = new ProtocolTypesDialogRadio();

            Bundle b  = new Bundle();
            b.putInt("position", twoPlayersProtocolTypePosition);

            alert.setArguments(b);
            alert.show(manager, "alert_dialog_radio");
		}
		else if (v == buttonOptions) 
		{
			startActivity(new Intent(MainScreen.this, PreferencesScreen.class));
		}
		else if (v == buttonBestScores) 
		{
			startActivity(new Intent(MainScreen.this, BestScoresScreen.class));
		}
		else if (v == buttonHowToPlay) 
		{
			startActivity(new Intent(MainScreen.this, HowToPlayScreen.class));
		}
		else if (v == buttonAbout) 
		{
			util.showToast(getApplicationContext(), R.string.AboutDesc, 2000, Constants.MAYA_ICON_ID);
		}
		else if (v == buttonExit) 
		{
			Intent intent = new Intent();
			intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
			
			intent.setAction(Intent.ACTION_MAIN);
			intent.addCategory(Intent.CATEGORY_HOME);

			startActivity(intent);
		}
		
		if(soundIsOn)
        	util.playSound(sound, getApplicationContext(), R.raw.click);
	}
	
	@Override
	public void onBackPressed() 
	{
		Intent intent = new Intent();
		intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
		
		intent.setAction(Intent.ACTION_MAIN);
		intent.addCategory(Intent.CATEGORY_HOME);

		startActivity(intent);
	}
	
    public void onPositiveClickInProtocolTypes(int position) 
    {
        this.twoPlayersProtocolTypePosition = position;
        
        if(Constants.TWO_PLAYERS_PROTOCOL_TYPES[this.twoPlayersProtocolTypePosition].equalsIgnoreCase("bluetooth"))
        {
        	startActivity(new Intent(MainScreen.this, Bluetooth.class));
        }
        else  if(Constants.TWO_PLAYERS_PROTOCOL_TYPES[this.twoPlayersProtocolTypePosition].equalsIgnoreCase("internet"))
        {
        	util.showToast(getApplicationContext(),R.string.UnderConstruction, 2000, Constants.WARN_ICON_ID);
        }
        else
        {
        	Toast.makeText(getBaseContext(), "olmayan protokol",Toast.LENGTH_SHORT).show();
        	
        }
         
    }
}